package com.wad.manager.controller;

import cn.hutool.http.server.HttpServerRequest;
import com.wad.dto.system.LoginDto;
import com.wad.entity.system.SysUser;
import com.wad.manager.service.SysMenuService;
import com.wad.manager.service.SysUserService;
import com.wad.manager.service.ValidateCodeService;
import com.wad.utils.AuthContextUtil;
import com.wad.vo.common.Result;
import com.wad.vo.common.ResultCodeEnum;
import com.wad.vo.system.LoginVo;
import com.wad.vo.system.SysMenuVo;
import com.wad.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wad
 * @date 2023/11/29 14:09
 * @project e-commerce
 **/

@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {


    @Resource
    private SysUserService sysUserService;

    @Resource
    private ValidateCodeService validateCodeService;

    @Resource
    private SysMenuService sysMenuService;



    //用户登录
    @PostMapping("/login")
    @Operation(summary = "登录的方法")
    public Result login(@RequestBody LoginDto loginDto){
        LoginVo loginVo=sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }


    //生成验证码接口
    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode(){
        ValidateCodeVo validateCodeVo=validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo,ResultCodeEnum.SUCCESS);
    }


    //获取当前登录的用户信息
    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        //从请求头中获取token
//        String token = request.getHeader("token");
//        根据token从redis中查找用户信息
//        SysUser sysUser=sysUserService.getUserInfo(token);
        //返回用户信息
        return Result.build(AuthContextUtil.get(),ResultCodeEnum.SUCCESS);
    }


    //用户退出
    @GetMapping("/logout")
    public Result logout(@RequestHeader(name = "token") String token){
        sysUserService.logout(token);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    //查询当前用户可以操作的菜单
    @GetMapping("/menus")
    public Result menus(){
       List<SysMenuVo> sysMenuVoList= sysMenuService.findMenuByUserId();
       return Result.build(sysMenuVoList,ResultCodeEnum.SUCCESS);
    }

}
